//
//  HeCertificationViewController.m
//  huien
//
//  Created by yiban on 2017/7/18.
//  Copyright © 2017年 lcb. All rights reserved.
//

#import "HeCertificationViewController.h"


@interface HeCertificationViewController ()<UITextFieldDelegate>
{
    
}

@property(nonatomic,strong) UIScrollView *scrollview;
@property(nonatomic,strong) UITextField     *txtOldPass;
@property(nonatomic,strong) UITextField     *txtNewPass;
@property(nonatomic,strong) UIButton     *okBtn;

@end

@implementation HeCertificationViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.navTitle=@"实名认证";
    [self initUI];
    
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
-(void)dealloc
{
    [self handleSingleTapFrom];
    self.txtOldPass=nil;
    self.txtNewPass=nil;
    self.okBtn=nil;
    
}
- (UIScrollView*)scrollview
{
    if (!_scrollview) {
        _scrollview =[[UIScrollView alloc] initWithFrame:CGRectMake(0, YB_HeaderHeight, SCREEN_WIDTH, SCREEN_HEIGHT-YB_HeaderHeight)];
        _scrollview.backgroundColor=[UIColor clearColor];
        UITapGestureRecognizer* singleRecognizer= [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapFrom)];
        _scrollview.userInteractionEnabled =YES;
        [_scrollview addGestureRecognizer:singleRecognizer];
        singleRecognizer=nil;
    }
    return _scrollview;
}
-(UITextField *)txtOldPass
{
    if (!_txtOldPass) {
        _txtOldPass = [[UITextField alloc] initWithFrame:CGRectZero];
        _txtOldPass.placeholder = @"请输入您的姓名";
        _txtOldPass.font = [UIFont systemFontOfSize:13];
        [_txtOldPass setValue:h2color forKeyPath:@"_placeholderLabel.textColor"];
        _txtOldPass.autocorrectionType = UITextAutocorrectionTypeNo;
        _txtOldPass.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _txtOldPass.returnKeyType = UIReturnKeyNext;
        _txtOldPass.clearButtonMode = UITextFieldViewModeWhileEditing;
        _txtOldPass.delegate=self;
        
    }
    return _txtOldPass;
}
-(UITextField *)txtNewPass
{
    if (!_txtNewPass) {
        _txtNewPass = [[UITextField alloc] initWithFrame:CGRectZero];
        _txtNewPass.placeholder = @"请输入您的身份证号";
        _txtNewPass.font = [UIFont systemFontOfSize:13];
        [_txtNewPass setValue:h2color forKeyPath:@"_placeholderLabel.textColor"];
        _txtNewPass.autocorrectionType = UITextAutocorrectionTypeNo;
        _txtNewPass.autocapitalizationType = UITextAutocapitalizationTypeNone;
        _txtNewPass.returnKeyType = UIReturnKeyDone;
        _txtNewPass.clearButtonMode = UITextFieldViewModeWhileEditing;
        _txtNewPass.delegate=self;
    }
    return _txtNewPass;
}
-(UIButton*)okBtn
{
    if (!_okBtn) {
        _okBtn =[UIButton buttonWithType:UIButtonTypeCustom];
        _okBtn.backgroundColor =btncolor;
        _okBtn.layer.cornerRadius = 45/2;
        _okBtn.tag= 1;
        [_okBtn setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
        [_okBtn setTitle:@"认证" forState:UIControlStateNormal];
        [_okBtn addTarget:self action:@selector(showBtnClick:) forControlEvents:UIControlEventTouchUpInside];
        [self.view addSubview:_okBtn];
    }
    return _okBtn;
}
-(void)showBtnClick:(id)sender
{
    UIButton *btn =(UIButton*)sender;
    switch (btn.tag) {
        case 0:
        {
        }
            break;
        case 1:
        {
            [self btnLoginAction];
        }
            break;
            
        default:
            break;
    }
}
-(void)handleSingleTapFrom
{
    if ([self.txtOldPass isFirstResponder]) {
        [self.txtOldPass resignFirstResponder];
    }
    if ([self.txtNewPass isFirstResponder]) {
        [self.txtNewPass resignFirstResponder];
    }
}
-(BOOL)textFieldShouldReturn:(UITextField *)textField{
    
    if (textField==self.txtOldPass) {
        [self.txtOldPass becomeFirstResponder];
    }else if (textField==self.txtNewPass){
        [self.txtNewPass resignFirstResponder];
    }else
    {
        [textField resignFirstResponder];
    }
    return YES;
}
-(void)initUI
{
    //
    UIButton *backButton = [UIButton buttonWithType:UIButtonTypeCustom];
    backButton.frame = CGRectMake(0, 20, 60, 44);
    backButton.tag =100;
    [backButton setImage:[UIImage imageNamed:@"back"] forState:UIControlStateNormal];
    backButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentLeft;
    [backButton addTarget:self action:@selector(clickNavEvent:) forControlEvents:UIControlEventTouchUpInside];
    [self.headerView addSubview:backButton];
    //
    [self.scrollview setContentSize:CGSizeMake(0, SCREEN_HEIGHT)];
    
    [self.view addSubview:self.scrollview];
    self.txtOldPass.frame =CGRectMake(20, 20, SCREEN_WIDTH-40, 40);
    UIView *lineView1 = [[UIView alloc]initWithFrame:CGRectMake(0,self.txtOldPass.height - 1, self.txtOldPass.width, 1)];
    lineView1.backgroundColor = linecolor;
    [self.txtOldPass addSubview:lineView1];
    [self.scrollview addSubview:self.txtOldPass];
    //
    self.txtNewPass.frame =CGRectMake(self.txtOldPass.frame.origin.x, self.txtOldPass.frame.origin.y+self.txtOldPass.frame.size.height+20,  SCREEN_WIDTH-40, 40);
    UIView *lineView2 = [[UIView alloc]initWithFrame:CGRectMake(0,self.txtNewPass.height - 1, self.txtNewPass.width, 1)];
    lineView2.backgroundColor = linecolor;
    [self.txtNewPass addSubview:lineView2];
    [self.scrollview addSubview:self.txtNewPass];

    //
    self.okBtn.frame=CGRectMake(self.txtNewPass.frame.origin.x, self.txtNewPass.frame.origin.y+self.txtNewPass.frame.size.height+45, SCREEN_WIDTH-40, 45);
    [self.scrollview addSubview:self.okBtn];
    
}
-(void)clickNavEvent:(id)sender
{
    UIButton *btn =(UIButton*)sender;
    switch (btn.tag) {
        case 100:
        {
            if (self.navigationController) {
                if (self.navigationController.viewControllers.count == 1) {
                    [self.navigationController dismissViewControllerAnimated:YES completion:nil];
                } else {
                    [self.navigationController popViewControllerAnimated:YES];
                }
            } else {
                [self dismissViewControllerAnimated:YES completion:nil];
            }
        }
            break;
        case 101:
        {
            
        }
            break;
        default:
            break;
    }
    
}

- (void)btnLoginAction
{
    if(self.txtOldPass.text.length==0)
    {
        [YBToast showWithText:@"请输入您的姓名"];
        return;
    }
    if(self.txtNewPass.text.length==0)
    {
        [YBToast showWithText:@"请输入您的身份证号"];
        return;
    }
    SessionModel *sessionModel =[HEShare sessionModel];
    __weak typeof (self) wSelf = self;
    NSMutableDictionary *dict =[NSMutableDictionary dictionary];
    [dict setValue:self.txtOldPass.text forKey:@"realname"];
    [dict setValue:self.txtNewPass.text forKey:@"idcard"];
    [dict setValue:sessionModel.sessionId forKey:@"sessionId"];
    [dict setValue:[NSString stringWithFormat:@"%ld",(long)sessionModel.session_user.userId] forKey:@"userId"];
    
    [HEApi idcardWithParam:dict serviceblock:^(id response, NSString *errorMsg) {
        if (response) {
            LcbBaseModel *sessionModel = (LcbBaseModel*)response;
            [YBToast showWithText:sessionModel.message];
            if (sessionModel.code==0) {
                [wSelf.navigationController popToRootViewControllerAnimated:YES];
            }
        }
        
    }];
    
}

@end
